home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / tptc16.zip / TPCMAC.H < prev    next >
C/C++ Source or Header  |  1993-01-04  |  5KB  |  236 lines

  1.  
  2.  
  3.  
  4. /*
  5.  * TPCMAC.H - Macro Header for use with Turbo Pascal --> C Translator
  6.  *
  7.  * S.H.Smith, 22-Dec-86 (rev. 05-Aug-87)
  8.  *
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdarg.h>
  15. #include <dos.h>
  16. #include <conio.h>
  17. #include <ctype.h>
  18.  
  19.  
  20. /* define some simple keyword replacements */
  21.  
  22. #define keypressed      kbhit()
  23. #define wherex          wherex()
  24. #define wherey          wherey()
  25. #define memavail        memavail()
  26. #define maxavail        maxavail()
  27. #define maxint          0x7fff
  28.  
  29. #define dispose(v)      free(v)
  30. #define pred(v)         ((v)-1)
  31. #define succ(v)         ((v)+1)
  32. #define sqr(v)          ((v)*(v))
  33. #define chr(n)          (n)
  34. #define ord(c)          (c)
  35. #define lo(v)           (v & 0xff)
  36. #define hi(v)           (v >> 8)
  37.  
  38. #define integer         int
  39. #define byte            char
  40. #define real            double
  41. #define boolean         int
  42.  
  43. #define false           0
  44. #define true            1
  45. #define nil             NULL
  46.  
  47. typedef FILE *text;
  48.  
  49. #define kbd             stdin
  50. #define input           stdin
  51. #define con             stdout
  52. #define output          stdout
  53.  
  54. #define eof(fd)         feof(fd)
  55. #define flush(fd)       fflush(fd)
  56. #define assign(fd,name) strcpy(_CURNAME,name)
  57. #define reset(fd)       ioresult = (( (fd = fopen(_CURNAME,"r")) == 0))
  58. #define rewrite(fd)     ioresult = (( (fd = fopen(_CURNAME,"w")) == 0))
  59. #define close(fd)       ioresult = fclose(fd)
  60.  
  61. #define upcase(c)       toupper(c)
  62. #define length(s)       strlen(s)
  63.  
  64. #define delete(s,p,num) strcpy(s+p-1,s+p+num)
  65. #define val(s,res,code) code=0, res=atof(s)
  66. #define str(val,res)    strcpy(res,ftoa((double)val));
  67.  
  68. typedef char *string;
  69. #define STRSIZ 255      /* default string length */
  70.  
  71.  
  72.  
  73.  
  74. /*
  75.  *   setof(a,b,...,-1)
  76.  *      construct and return a set of the specified character values
  77.  *
  78.  *   inset(ex,set)
  79.  *      predicate returns true if expression ex is a member of
  80.  *      the set parameter
  81.  *
  82.  */
  83. #define THRU            -2
  84. #define ENDSET          -1
  85.  
  86.  
  87.  
  88.  
  89. /*
  90.  * copy len bytes from the dynamic string dstr starting at position from
  91.  *
  92.  */
  93. string copy(string str,
  94.             int    from,
  95.             int    len)
  96. {
  97.    static char buf[STRSIZ];
  98.    buf[0]=0;
  99.    if (from>strlen(str))     /* copy past end gives null string */
  100.       return buf;
  101.  
  102.    strcpy(buf,str+from-1);  /* skip over first part of string */
  103.    buf[len] = 0;            /* truncate after len characters */
  104.    return buf;
  105. }
  106.  
  107.  
  108. /*
  109.  * String/character concatenation function
  110.  *
  111.  * This function takes a sprintf-like control string, a variable number of
  112.  * parameters, and returns a pointer a static location where the processed
  113.  * string is to be stored.
  114.  *
  115.  */
  116.  
  117. string scat(string control, ...)
  118. {
  119.    static char buf[STRSIZ];
  120.    char buf2[STRSIZ];
  121.    va_list args;
  122.  
  123.    va_start(args, control);     /* get variable arg pointer */
  124.    vsprintf(buf2,control,args); /* format into buf with variable args */
  125.    va_end(args);                /* finish the arglist */
  126.  
  127.    strcpy(buf,buf2);
  128.    return buf;                  /* return a pointer to the string */
  129. }
  130.  
  131.  
  132. #define ctos(ch) scat("%c",ch)  /* character to string conversion */
  133.  
  134.  
  135. /*
  136.  * string build - like scat, sprintf, but will not over-write any
  137.  *                input parameters
  138.  */
  139.  
  140. int sbld(string dest,
  141.          string control, ...)
  142. {
  143.    char buf[STRSIZ];
  144.    va_list args;
  145.  
  146.    va_start(args, control);     /* get variable arg pointer */
  147.    vsprintf(buf,control,args);  /* format into buf with variable args */
  148.    va_end(args);                /* finish the arglist */
  149.  
  150.    strcpy(dest,buf);            /* copy result */
  151. }
  152.  
  153.  
  154.  
  155. /*
  156.  * spos(str1,str2) - returns index of first occurence of str1 within str2;
  157.  *    1=first char of str2
  158.  *    0=nomatch
  159.  */
  160.  
  161. int spos(string str1,
  162.          string str2)
  163. {
  164.         string res;
  165.         res = strstr(str2,str1);
  166.         if (res == NULL)
  167.            return 0;
  168.         else
  169.            return res - str2 + 1;
  170. }
  171.  
  172.  
  173. /*
  174.  * cpos(str1,str2) - returns index of first occurence of c within str2;
  175.  *    1=first char of str2
  176.  *    0=nomatch
  177.  */
  178.  
  179. int cpos(char c,
  180.          string str2)
  181. {
  182.         string res;
  183.         res = strchr(str2,c);
  184.         if (res == NULL)
  185.            return 0;
  186.         else
  187.            return res - str2 + 1;
  188. }
  189.  
  190.  
  191.  
  192. /*
  193.  * Scanf/Fscanf support
  194.  *
  195.  * These functions operate like scanf and fscanf except for an added control
  196.  * code used for full-line reads.
  197.  *
  198.  */
  199.  
  200. int fscanv(text fd,
  201.            string control, ...)
  202. {
  203.    va_list args;
  204.    string  arg1;
  205.    int     i;
  206.  
  207.    va_start(args, control);     /* get variable arg pointer */
  208.  
  209.    /* process special case for full-line reads (why doesn't scanf allow
  210.       full-line string reads?  why don't gets and fgets work the same?) */
  211.    if (*control == '#') {
  212.         arg1 = va_arg(args,string);
  213.         fgets(arg1,STRSIZ,fd);
  214.         arg1[strlen(arg1)-1] = 0;
  215.         return 1;
  216.    }
  217.  
  218.    /* pass the request on to fscanf */
  219.    i = vfscanf(fd,control,args);    /* scan with variable args */
  220.    va_end(args);                    /* finish the arglist */
  221.  
  222.    return i;                        /* return a pointer to the string */
  223. }
  224.  
  225.  
  226.  
  227. /* 
  228.  * file access support 
  229.  */
  230.  
  231. char _CURNAME[64];
  232. int  ioresult = 0;
  233.  
  234. #undef atoi         /* in case of user ident clash */
  235. #undef getchar
  236.